package com.gsm.controller;

import com.gsm.entity.Result;
import com.gsm.entity.SysUser;
import com.gsm.po.Subject;
import com.gsm.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 科目(Subject)表控制层
 *
 * @author wjq
 * @since 2020-11-27 00:42:54
 */
@RestController
@RequestMapping("subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 新增
     */
    @PostMapping("subject")
    public Result insertSubject(@RequestBody Subject subject,@RequestAttribute("user") SysUser u) {
        Result result = subjectService.insertSubject(subject,u);
        return result;
    }

    /**
     * 根据 id 删除数据
     */
    @DeleteMapping("subject/{subjectId}")
    public Result deleteSubjectBySubjectId(@PathVariable("subjectId") Long subjectId,@RequestAttribute("user") SysUser u) {
        Result result = subjectService.deleteSubjectBySubjectId(subjectId,u);
        return result;
    }

    /**
     * 根据 id 修改单条数据
     */
    @PutMapping("subject/{subjectId}")
    public Result updateSubjectBySubjectId(@PathVariable("subjectId") Long subjectId, @RequestBody Subject subject,@RequestAttribute("user") SysUser u) {
        subject.setSubjectId(subjectId);
        Result result = subjectService.updateSubjectBySubjectId(subject,u);
        return result;
    }

    /**
     * 根据 id 查询单条数据
     */
    @GetMapping("subject/{subjectId}")
    public Result selectSubjectBySubjectId(@PathVariable("subjectId") Long subjectId) {
        Result result = subjectService.selectSubjectBySubjectId(subjectId);
        return result;
    }

    /**
     * 根据条件查询 List
     */
    @GetMapping("subjectList")
    public Result selectSubjectList(Subject subject,@RequestAttribute("user") SysUser u) {
        Result result = subjectService.selectSubjectList(subject,u);
        return result;
    }

    /**
     * 根据id排序
     */
    @PutMapping("order")
    public Result updateSubjectShowOrder(@RequestBody List<Long> subjectIds,@RequestAttribute("user") SysUser u) {
        Result result = subjectService.updateSubjectShowOrder(subjectIds,u);
        return result;
    }

    /**
     * 刷新
     */
    @GetMapping("refresh")
    public Result refresh() {
        Result result = subjectService.refresh();
        return result;
    }

    /**
     * 刷新
     */
    @GetMapping("refresh/{subjectId}")
    public Result refreshBySubjectId(@PathVariable("subjectId") Long subjectId) {
        Result result = subjectService.refreshBySubjectId(subjectId);
        return result;
    }
}